# GF_PY3_CLASS_Finance_Indicator_SMA_s_Breakpoint_Recovery.py
# Create By GF 2024-01-22 18:16

import json
# ..................................................
import GF_PY3_CLASS_Python_3_List_and_JSON_File
import GF_PY3_CLASS_Finance_Indicator_SMA

# ##################################################

class Finance_Indicator_SMA_s_Breakpoint_Recovery(GF_PY3_CLASS_Python_3_List_and_JSON_File.Python_3_List_and_JSON_File,
                                                  GF_PY3_CLASS_Finance_Indicator_SMA.Finance_Indicator_SMA):

    # Examples:
    # GF_PY3_CLASS_Finance_Indicator_Calculation_Breakpoint.json 文件内容:
    #     [{"Table": "stocks_en_us", "Time": "2025-04-02", "Code": "600100.SH",  "SMA5": 7.814, "Queue_of_Close": [7.93, 7.82, 7.79, 7.78, 7.75]},
    #      {"Table": "stocks_en_us", "Time": "2025-04-02", "Code": "600100.SH", "SMA10": 7.956, "Queue_of_Close": [8.42, 8.16, 8.01, 7.96, 7.94, 7.93, 7.82, 7.79, 7.78, 7.75]}]
    # 测试数据 (600100.SH):
    #     2025-04-03, 600100.SH, Close = 7.69, SMA5 = 7.766
    #     2025-04-07, 600100.SH, Close = 6.92, SMA5 = 7.586
    #     2025-04-08, 600100.SH, Close = 6.78, SMA5 = 7.384
    # 其中的 SMA5 = 7.766, SMA5 = 7.586, SMA5 = 7.384 为验证用途 (验证计算出的 SMA5 是否和验证数值相等)
    # >>>
    # >>> Func = Finance_Indicator_SMA_s_Breakpoint_Recovery()
    # >>>
    # >>> Func.SMA_s_Breakpoint_Recovery(Table = "stocks_en_us", Time = "2025-04-03", Code = "600100.SH", ROW_NUM = 2, MAX_ROW_NUM = 4, Period = 5, Close = 7.69)
    # 7.766
    # >>> Func.SMA_s_Breakpoint_Recovery(Table = "stocks_en_us", Time = "2025-04-07", Code = "600100.SH", ROW_NUM = 3, MAX_ROW_NUM = 4, Period = 5, Close = 6.92)
    # 7.586
    # >>> Func.SMA_s_Breakpoint_Recovery(Table = "stocks_en_us", Time = "2025-04-08", Code = "600100.SH", ROW_NUM = 4, MAX_ROW_NUM = 4, Period = 5, Close = 6.78)
    # 7.384

    def __init__(self):

        self.Breakpoint_Exists:int = 0
        self.Historical_Queue_of_Close:list = []
        # ..........................................
        self.JSON_Queue_of_Close:list = []

    def SMA_s_Breakpoint_Recovery(self, Table:str, Time:str, Code:str, ROW_NUM:int, MAX_ROW_NUM:int, Period:int, Close:float) -> float:

        SMA_Value:float = None

        if (ROW_NUM == 2):

            JSON_List = self.JSON_File_to_Python_3_List("./GF_PY3_CLASS_Finance_Indicator_Calculation_Breakpoint.json")
            JSON_List = [Line for Line in JSON_List if ( Line.get("Table", '') == Table)  ]
            JSON_List = [Line for Line in JSON_List if ( Line.get("Code",  '') == Code )  ]
            JSON_List = [Line for Line in JSON_List if ( f"SMA{Period}"    in Line.keys())]
            JSON_List = [Line for Line in JSON_List if ( f"Queue_of_Close" in Line.keys())]
            # ......................................
            JSON_Dict = JSON_List[-1] if (JSON_List != []) else {}
            # ......................................
            self.Historical_Queue_of_Close = JSON_Dict.get(f"Queue_of_Close", [])
            # ......................................
            if (self.Historical_Queue_of_Close == []): self.Breakpoint_Exists = 0
            if (self.Historical_Queue_of_Close != []): self.Breakpoint_Exists = 1

        if (self.Breakpoint_Exists == 0):

            SMA_Value = self.SMA(ROW_NUM = (ROW_NUM - 1), Period = Period, Close = Close)

        if (self.Breakpoint_Exists == 1 and ROW_NUM == 2):  # 仅在 "ROW_NUM == 2" 时拷贝历史计算数据

            self.JSON_Queue_of_Close = self.Historical_Queue_of_Close.copy()  # 拷贝 (非引用)

        if (self.Breakpoint_Exists == 1):

            SMA_Value = self.SMA(ROW_NUM = (ROW_NUM + Period - 1), Period = Period, Close = Close)

        if (ROW_NUM == MAX_ROW_NUM):

            JSON_List = self.JSON_File_to_Python_3_List("./GF_PY3_CLASS_Finance_Indicator_Calculation_Breakpoint.json")
            JSON_List.append({"Table": Table, "Time":Time, "Code": Code, f"SMA{Period}": SMA_Value, "Queue_of_Close": self.JSON_Queue_of_Close})
            # ......................................
            self.Python_3_List_to_JSON_File(Python_3_List = JSON_List, JSON_File = "./GF_PY3_CLASS_Finance_Indicator_Calculation_Breakpoint.json")

        return SMA_Value

# EOF Signed by GF.
